<template>
  <div class="container">
    <div class="app-container">
      <el-tree default-expand-all :data="depts" :props="defaultProps" :expand-on-click-node="false">
        <!-- 节点结构 -->
        <!-- v-slot="{ node, data }" 只能作用在template -->
        <template v-slot="{ data }">
          <el-row style="width:100%;height:40px" type="flex" justify="space-between" align="middle">
            <el-col>{{ data.name }}</el-col>
            <el-col :span="4">
              <span class="tree-manager">{{ data.managerName }}</span>
              <!-- $event 实参 表示类型 --> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
              <el-dropdown @command="operateDept($event,data.id)">
                <!-- 显示区域内容 -->
                <span class="el-dropdown-link">
                  &nbsp;操作<i class="el-icon-arrow-down el-icon--right" />
                  <!-- 下拉菜单选项 -->
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                    <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                    <el-dropdown-item command="del">删除</el-dropdown-item>
                  </el-dropdown-menu>
                </span></el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>
      <!-- 下面第一个调用了close函数 -->
      <!-- <add-dept :show-dialog="showDialog" @update:showDialog="close" /> -->
      <!-- 这个相当于这个语法糖 -->
      <add-dept ref="addDept" :show-dialog.sync="showDialog" :addid="addid" :addtitle="addtitle" @updateDepart="getDepartment" />
      <!-- 这个语法糖实际就是 -->
      <!-- <add-dept :show-dialog="showDialog" @update:showDialog="showDialog=$event" /> -->
    </div>
  </div>
</template>
<script>
// 引入获取数据
import { getDepartment, delOneDepartment } from '@/api/department'
// 引入处理数据
import { ProcessingData } from '@/utils/index'
// 引入弹窗组件
import AddDept from './components/add-dept.vue'
export default {
  name: 'Department',
  components: {
    // 导入组件
    AddDept
  },
  data() {
    return {
      addtitle: '新增部门',
      showDialog: false,
      addid: -1,
      depts: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  created() {
    // 创建页面后更新数据
    this.getDepartment()
  },
  methods: {
    // 获取部门数据
    async getDepartment() {
      const result = await getDepartment()
      // console.log(result)
      this.depts = ProcessingData(result.data, 0)
    },
    // 操作传参开关弹出窗和删除
    async  operateDept(type, id) {
      // console.log(type)
      if (type === 'add') {
        this.addtitle = '新增部门'
        this.showDialog = true
      }
      if (type === 'edit') {
        this.addtitle = '编辑部门'
        this.showDialog = true
      }
      if (this.showDialog === false) {
        console.log(id)
        await this.$confirm('您确定删除吗', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
          // 发送请求 完成删除
        })
        await delOneDepartment(id)
        this.$message.success('删除成功')
        this.getDepartment()
      }
      this.addid = id
    }
    // close() {
    //   this.showDialog = false
    // }

  }

}
</script>
<style scoped>
.app-container {
  padding: 30px 140px;
  font-size: 14px;
}
.tree-manager {
  width: 50px;
  display: inline-block;
  margin: 10px;
}
</style>
